package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ManBean;
import com.project.mapper.IManMapper;
import com.project.service.IManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class ManServiceImpl implements IManService {
    @Autowired
    private IManMapper mapper;
    @Override
    public void add(ManBean man) {
        mapper.insert(man);
    }

    @Override
    public void del(Integer id) {
        mapper.deleteById(id);
    }

    @Override
    public void update(Integer id, String phone) {
        ManBean man = mapper.selectById(id);
        man.setPhone(phone);
        mapper.updateById(man);
    }

    @Override
    public ManBean findById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public List<ManBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<ManBean> qw = new QueryWrapper<>();
        if(name != null && name.length() != 0){
            qw.like("m_name",name);
        }
        if(startDate != null){
            qw.ge("m_birthday",startDate);
        }
        if(endDate != null){
            qw.le("m_birthday",endDate);
        }

        return mapper.selectList(qw);
    }

    @Override
    public IPage<ManBean> cutAll(Integer pageNO) {
        return mapper.selectPage(new Page(pageNO,3),null);
    }

    @Override
    public IPage<ManBean> cutByItem(Integer pageNO, String name, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<ManBean> qw = new QueryWrapper<>();
        if(name != null && name.length() != 0){
            qw.like("m_name",name);
        }
        if(startDate != null){
            qw.ge("m_birthday",startDate);
        }
        if(endDate != null){
            qw.le("m_birthday",endDate);
        }

        return mapper.selectPage(new Page(pageNO,3),qw);
    }
}
